#ifndef RBT_CONVERT_INCLUDE
#define RBT_CONVERT_INCLUDE

#include "../typedef.h"
#include "Text.h"
#include <sstream>
#include <cerrno>
RETURNCODE E_CONVERT_MAXSIZE = -336;
RETURNCODE E_CONVERT_MTOW    = -337;
RETURNCODE E_CONVERT_WTOM    = -338;





namespace Convert
{

	template< class E , class T , class A , class V>
	int32 ToValue(const std::basic_string< E , T  , A >& source , V& value )
	{
		std::basic_stringstream< E , T , A > sstream;
		sstream<<source;
		if( !(sstream>>value))
		{
			return R_FAILED;
		}

		if(errno == ERANGE)
			return R_FAILED;
		return R_SUCCEED;
	}

	template< class E , class T , class A , class V >
	int32 ToString( const V& source , std::basic_string<E,T,A>& value )
	{
		std::basic_stringstream< E , T , A > sstream;
		sstream<<source;
		if( !(sstream>>value) )
		{
			return R_FAILED;
		}

		if( errno == ERANGE )
			return R_FAILED;
		return R_SUCCEED;
	}

	inline int32 ToInt32( cstring& source , int32& value )
	{
		return ToValue(source,value);
	}

	inline int32 ToUInt32( cstring& source , uint32& value )
	{
		return ToValue( source , value );
	}

	inline int32 ToUInt64( cstring& source , uint64& value )
	{
		return ToValue(source,value);
	}

	inline int32 ToInt64( cstring& source , int64& value )
	{
		return ToValue(source,value);
	}

	inline int32 ToFloat64( cstring& source , float64& value )
	{
		return ToValue(source,value);
	}

	int32 UnicodeToAnsi( const std::wstring source , std::string& value );
	int32 AnsiToUnicode( const std::string source , std::wstring& value );
	int32 AnsiToUTF8( const std::string source , std::string& value );

	inline int32 ToString( cstring& source , string& value )
	{
		value = source;
		return R_SUCCEED;
	}

	int32 ToString( const byte* source , uint32 count , string& value );

#ifdef RBT_UNICODE
	inline int32 ToString( const std::string& source , string& value )
	{
		return AnsiToUnicode(source,value);
	}
#else
	inline INT32 ToString(const std::wstring& source , string& value)
	{
		return UnicodeToAnsi(source,value);
	}
#endif
}





#endif